package edu.info.manager.controller;

import edu.info.manager.domain.Teacher;
import edu.info.manager.service.TeacherService;

import javax.print.DocFlavor;
import java.util.Scanner;

public class TeacherController {
    private Scanner sc = new Scanner(System.in);
    private TeacherService teacherService = new TeacherService();

    public void start() {

        String choice;
        tl:
        while (true) {
            System.out.println("--------欢迎来到 <老师> 管理系统--------");
            System.out.println("请输入您的选择: 1.添加老师  2.删除老师  3.修改老师  4.查看老师  5.退出");
            choice = sc.next();
            switch (choice) {
                case "1":
                    //System.out.println("添加老师");
                    addTeacher();
                    break;
                case "2":
                    //System.out.println("删除老师");
                    deleteTeacherById();
                    break;
                case "3":
                    //System.out.println("修改老师");
                    updateTeacher();
                    break;
                case "4":
                    //System.out.println("查看老师");
                    findAllTeachers();
                    break;
                case "5":
                    System.out.println("感谢你的使用！");
                    break tl;
                default:
                    System.out.println("你输入有误，请重新输入");
                    break;
            }
        }

    }

    private void updateTeacher() {
        String id = inputTeacherId();
        System.out.println("请输入老师姓名：");
        String name = sc.next();
        System.out.println("请输入老师年龄：");
        String age = sc.next();
        System.out.println("请输入老师生日：");
        String birthday = sc.next();

        Teacher teacher = new Teacher(id, name, age, birthday);
        teacherService.updateTeacherById(id,teacher);
        System.out.println("修改成功");
    }

    private String inputTeacherId(){
        String id;
        while(true){
            System.out.println("请输入老师编号：");
            id = sc.next();
            if ("-1".equals(id)){
                return "-1";
            }
            boolean exist = teacherService.isExist(id);
            if(!exist){
                System.out.println("查无此人，请重新输入：");
            }else{
                break;
            }
        }

        return id;
    }

    private void deleteTeacherById() {
        String id = inputTeacherId();

        //if id = -1,then  exit
        if ("-1".equals(id)){
            return;
        }
        teacherService.deleteTeacherById(id);
        System.out.println("删除成功");

    }

    private void findAllTeachers() {
        Teacher[] teas = teacherService.findAllTeacers();

        if(teas == null){
            System.out.println("查无信息，请添加后重试");
            return;
        }

        System.out.println("编号\t姓名\t年龄\t生日");
        for (int i = 0; i < teas.length; i++) {
            Teacher tea = teas[i];
            if(tea != null){
                System.out.println(tea.getId()+"\t"+tea.getName()+"\t"+tea.getAge()+"\t"+tea.getBirthday());
            }
        }

    }

    private void addTeacher() {
        String id;
        while(true){
            System.out.println("请输入老师编号：");
            id = sc.next();
            boolean exist = teacherService.isExist(id);
            if(exist){
                System.out.println("编号已被占用，请重新输入：");
            }else {
                break;
            }
        }

        System.out.println("请输入老师姓名：");
        String name = sc.next();
        System.out.println("请输入老师年龄：");
        String age = sc.next();
        System.out.println("请输入老师生日：");
        String birthday = sc.next();

        Teacher teacher = new Teacher(id, name, age, birthday);
        boolean flag = teacherService.addTeacher(teacher);

        if (flag) {
            System.out.println("添加成功");
        } else {
            System.out.println("添加失败");
        }
    }
}
